package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con ;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public Integer saveOrders(String userId, Integer businessId, Integer daId, Double orderTotal) throws SQLException {
        int result = 0;
        String sql = "insert into orders(userId,businessId,orderDate,daId,orderTotal,orderState) values(?,?,?,?,?,?)";
        try{
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            String orderDate = CommonUtil.getCurrentDate();
            pst.setString(1, userId);
            pst.setInt(2, businessId);
            pst.setString(3, orderDate);
            pst.setInt(4, daId);
            pst.setDouble(5, orderTotal);
            pst.setInt(6,0);
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            while (rs.next()){
                result = rs.getInt(1);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws SQLException {
        // 当前SQL语句只是查询到了订单和对应的商家，并没有查询到订单详细信息
        String sql = "SELECT * from orders,business WHERE orders.businessId=business.businessId and orders.orderId=?";
        List<Orders> list = new ArrayList<>();
        Orders orders = new Orders();
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderId);
            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();

            while (rs.next()){
                Business business =new Business();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                orders.setBusiness(business);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws SQLException {
        // 当前SQL语句只是根据当前用户编号查询到了订单和对应的商家，并没有查询到订单详细信息
        // 因为一个用户有多个订单，所以返回的是list集合
        String sql = "SELECT * from orders,business WHERE orders.businessId=business.businessId and orders.userId=?";
        List<Orders> list = new ArrayList<>();
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,userId);
            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                Business business =new Business();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                orders.setBusiness(business);
                list.add(orders);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return list;
    }
}
